package com.yc.ioc.test.bean2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import java.util.concurrent.*;

@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages="com.yc.test.bean2")
public class AppConfig {

    @Value("${thread.pool.corePoolSize}")
    private int corePoolSize;

    @Value("${thread.pool.maximumPoolSize}")
    private int maximumPoolSize;

    @Value("${thread.pool.keepAliveTime}")
    private long keepAliveTime;

    @Value("${thread.pool.workQueueCapacity}")
    private int workQueueCapacity;

    @Bean
    public ThreadPoolExecutor tpe(){
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(workQueueCapacity);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
        return executor;
    }
}
